﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Windows.Forms;

using Kitware.VTK;

namespace Fu3DEngine
{
   public class DataSource
    {

       //这是一个测试时，暂时使用的方法
       public static vtkImageData GetImageFromGridBinary()
       {
           BinaryFormatter bf = new BinaryFormatter();

           Stream stream = new FileStream(@"c:\3dtest\gridpoint", FileMode.Open, FileAccess.ReadWrite, FileShare.None);

           double[,] mydata;


           try
           {

               mydata = (double[,])bf.Deserialize(stream);

               stream.Close();



           }
           catch (Exception ex)
           {
               MessageBox.Show("错误：" + ex.Message);
               return null;

               stream.Close();

           }



           //for (int i = 0; i < mydata.Length; i++)
           //{


           //}

           vtkDoubleArray array = vtkDoubleArray.New();
           array.SetName("3ddata");
           array.SetNumberOfComponents(1);
           array.SetNumberOfTuples(mydata.Length);



           for (int i = 0; i < 25; i++)
           {

               for (int j = 0; j < 25; j++)
               {
                   array.SetTuple1(i * 25 + j, mydata[(24-i), j]);
               }

           }


           vtkImageData img = vtkImageData.New();
           img.GetPointData().SetScalars(array);
           img.SetDimensions(25, 25, 1);

           img.SetScalarTypeToDouble();

           img.SetNumberOfScalarComponents(1);
           img.SetSpacing(480, 480, 1);
           img.SetOrigin(0, 0, 0);


           //哥，据说这个都是值传递，你可别骗我啊

           return img;


       }


       /// <summary>
       /// 这个是测试多scalar用的
       /// </summary>
       /// <returns></returns>

          public static vtkImageData GetImageFromGridBinary2()
          {
              BinaryFormatter bf = new BinaryFormatter();

              Stream stream = new FileStream(@"c:\3dtest\gridpoint", FileMode.Open, FileAccess.ReadWrite, FileShare.None);

              double[,] mydata;


              try
              {

                  mydata = (double[,])bf.Deserialize(stream);

                  stream.Close();



              }
              catch (Exception ex)
              {
                  MessageBox.Show("错误：" + ex.Message);
                  return null;

                  stream.Close();

              }



              //for (int i = 0; i < mydata.Length; i++)
              //{


              //}

              vtkDoubleArray array = vtkDoubleArray.New();
              array.SetName("3ddata");
              array.SetNumberOfComponents(2);
              array.SetNumberOfTuples(mydata.Length);



              for (int i = 0; i < 25; i++)
              {

                  for (int j = 0; j < 25; j++)
                  {
                      //array.SetTuple1(i * 25 + j, mydata[(24 - i), j]);
                      array.SetTuple2(i * 25 + j, mydata[(24 - i), j],1.0);
                  }

              }


              vtkImageData img = vtkImageData.New();
              img.GetPointData().SetScalars(array);
              img.SetDimensions(25, 25, 1);

              img.SetScalarTypeToDouble();
              

              img.SetNumberOfScalarComponents(2);
              img.SetSpacing(480, 480, 1);
              img.SetOrigin(0, 0, 0);


              //哥，据说这个都是值传递，你可别骗我啊

              return img;
            
          }


       public static vtkImageData GetImageFromCanvas()
       {
           vtkImageCanvasSource2D imgs = vtkImageCanvasSource2D.New();
           imgs.SetScalarTypeToFloat();
           imgs.SetExtent(0, 255, 0, 255, 0, 0);
           imgs.SetDrawColor(1, 1, 1);
           imgs.FillBox(0, 255, 0, 255);
           imgs.SetDrawColor(0, 0, 1);
           imgs.FillBox(75, 175, 75, 175);
           imgs.SetDrawColor(1, 0, 0);
           imgs.FillTriangle(86, 150, 96, 140, 160, 130);

           return imgs.GetOutput();
       }

       public static vtkImageData GetImageFromDem(string fname)
       {
           vtkDEMReader dem = vtkDEMReader.New();
           dem.SetFileName(fname);
           dem.Update();

           return dem.GetOutput();
       }


       public static vtkImageData GetImageFromGrid()
       {
           vtkImageGridSource grid = vtkImageGridSource.New();
           grid.SetDataScalarTypeToDouble();
           grid.SetGridOrigin(0,0,0);
           grid.SetGridSpacing(1,1,1);
           grid.SetFillValue(0);

           grid.SetDataExtent(0,255,0,255,0,0);

           return grid.GetOutput();

       }

    }
}
